<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON ) {
                var deletItemsCount = 0;
                dutysystemRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#dutysystemGird").jqGrid('getGridParam', 'selrow');
                        var dutysystem = jQuery("#dutysystemGird").jqGrid('getRowData', rowid);
                        dijit.byId('dutysystemUFD').setValues(dutysystem);
                    }
                };
                topic.subscribe("dutysystem/insertOrUpdate", dutysystemRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response == '"success"' ? "成功" : "失败",
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#dutysystemGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#dutysystemGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#dutysystemGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#dutysystemGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                dutysystemSubmitForm = function(formId, type) {
                    var url = "dutysystem/insert";
                    if (type == 'u') {
                        url = "dutysystem/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues(), true);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("dutysystem/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                dutysystemDelete = function() {
                    var ids = jQuery("#dutysystemGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var dutysystem = {};
                            dutysystem.id = '';
                            array.forEach(ids, function(entry, i) {
                                dutysystem.id += entry + ",";
                            });
                            syncDB("dutysystem/delete", JSON.stringify(dutysystem, true));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                dutysystemQuery = function(formId) {
                    var dutysystem = dijit.byId(formId).getValues();
                    jQuery("#dutysystemGird").jqGrid('setGridParam', {
                        postData : dutysystem
                    }).trigger('reloadGrid');
                };
                showDutySectionPanel =  function(){
                    dijit.byId('dutysystemSection').show();
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="dutysystemIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="systemname">班制名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="systemname"></td>
      </tr>
      <tr>
        <td><label for="systemsetting">班次: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="" type="text"
          name="systemsetting" onclick="showDutySectionPanel();"></td>
      </tr>
      <tr>
        <td><label for="systemdesc">班次描述: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="systemdesc"></td>
      </tr>
      <tr>
        <td><label for="isdefault">是否缺省: </label></td>
        <td> <section data-dojo-type="dijit/form/Select" style="width: 100px;" name="isdefault">
            <option value="1">是</option>
            <option value="0">否</option>
          </section></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="dutysystemSubmitForm('dutysystemIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="dutysystemRestForm('dutysystemIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="dutysystemUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="systemid" style="display: none;">
    <table>
      <tr>
        <td><label for="systemname">班制名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="systemname"></td>
      </tr>
      <tr>
        <td><label for="systemsetting">班次: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="systemsetting"></td>
      </tr>
      <tr>
        <td><label for="systemdesc">班次描述: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="systemdesc"></td>
      </tr>
      <tr>
        <td><label for="isdefault">是否缺省: </label></td>
        <td> <section data-dojo-type="dijit/form/Select" style="width: 100px;" name="isdefault">
            <option value="1">是</option>
            <option value="0">否</option>
          </section></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="dutysystemSubmitForm('dutysystemUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="dutysystemRestForm('dutysystemUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryDutySystemForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="systemname">班制名称: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="systemname"></td>
           <td><label for="systemsetting">班次: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="systemsetting"></td>
           <td><label for="systemdesc">班次描述: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="systemdesc"></td>
           <td><label for="isdefault">是否缺省: </label></td>
          <td>
          <section data-dojo-type="dijit/form/Select" style="width: 100px;" name="isdefault">
            <option value="">&nbsp;</option>
            <option value="1">是</option>
            <option value="0">否</option>
          </section>
          </td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="dutysystemInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('dutysystemIFD').show();
      </script>
    </button>
    <button id="dutysystemDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="dutysystemDelete();"></button>
    <button id="dutysystemUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#dutysystemGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var dutysystem = jQuery("#dutysystemGird").jqGrid('getRowData', ids[0]);
          dijit.byId('dutysystemUFD').setValues(dutysystem);
          dijit.byId('dutysystemUFD').show();
        }
      </script>
    </button>
    <button id="dutysystemQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="dutysystemQuery('queryDutySystemForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="dutysystemGird"></table>
  <div id="dutysystemToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#dutysystemGird").jqGrid({
        url : 'dutysystem/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '班制',
          '班制名称',
          '班次',
          '班次描述',
          '是否缺省：',
          '缺省：'
        ],
        colModel : [ 
        {
          name : 'systemid',
          index : 'systemid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'systemname',
          width : 200,
          index : 'systemname'
        },
        {
          name : 'systemsetting',
          width : 200,
          index : 'systemsetting'
        },
        {
          name : 'systemdesc',
          width : 450,
          index : 'systemdesc'
        },
        {
          name : 'isdefault',
          hidden : true,
          width : 100,
          index : 'isdefault'
        },
        {
          name : 'default_',
          width : 100,
          index : 'default_'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#dutysystemToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },gridComplete : function() {
            var ids = jQuery("#dutysystemGird").jqGrid('getDataIDs');
            for ( var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                var carcheck = jQuery("#dutysystemGird").jqGrid('getRowData', cl);
                var status = carcheck['isdefault'] == '1' ? '是' : '否';
                jQuery("#dutysystemGird").jqGrid('setRowData', cl, {
                     default_ : status,
                });
            }
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "班制表列表"
    });
    jQuery("#dutysystemGird").jqGrid('navGrid', '#dutysystemToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>

<div data-dojo-type="dijit/Dialog" id="dutysystemSection" title=" 班次选择">
  <div class="dijitDialogPaneContentArea">
    <div id="gird">
        <table id="dutysectionGird"></table>
        <div id="dutysectionToolbar"></div>
    </div>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="alert(0)">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="alert(1)">取消</button>
  </div>
</div>
<script type="text/javascript">
    jQuery("#dutysectionGird").jqGrid({
        url : 'dutysection/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 350,
        colNames : [ '班次ID', '班次名称', '开始签到', '上班时间', '签到结束时间', '早退计时', '下班时间', '签退结束' ],
        colModel : [ {
            name : 'sectionid',
            index : 'sectionid',
            width : 60,
            key : true,
            hidden : true,
            align : 'center'
        }, {
            width : 140,
            name : 'sectionname',
            index : 'sectionname'
        }, {
            width : 140,
            name : 'startsignin',
            index : 'startsignin'
        }, {
            width : 140,
            name : 'dutystarttime',
            index : 'dutystarttime'
        }, {
            width : 140,
            name : 'endsignin',
            index : 'endsignin'
        }, {
            width : 140,
            name : 'earlyofftime',
            index : 'earlyofftime'
        }, {
            width : 140,
            name : 'dutyendtime',
            index : 'dutyendtime'
        }, {
            width : 140,
            name : 'signouttime',
            index : 'signouttime'
        } ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#dutysectionToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "班次表列表"
    });
    jQuery("#dutysectionGird").jqGrid('navGrid', '#dutysectionToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
